home *** CD-ROM | disk | FTP | other *** search
- package sun.tools.zip;
-
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.util.Enumeration;
- import java.util.Hashtable;
-
- public class ZipFile implements ZipConstants {
- private RandomAccessFile file;
- private String path;
- private Hashtable table;
- long cenpos;
- private long endpos;
- private long pos;
- private static final int INBUFSIZ = 64;
-
- public ZipFile(String var1) throws ZipFormatException, IOException {
- this.file = new RandomAccessFile(var1, "r");
- this.path = var1;
- this.init();
- }
-
- public ZipFile(File var1) throws ZipFormatException, IOException {
- this(var1.getPath());
- }
-
- public ZipEntry getEntry(String var1) {
- return (ZipEntry)this.table.get(var1);
- }
-
- public InputStream getInputStream(ZipEntry var1) throws ZipFormatException, IOException {
- return new ZipFileInputStream(this, var1);
- }
-
- public String getPath() {
- return this.path;
- }
-
- public Enumeration entries() {
- return this.table.elements();
- }
-
- public void close() throws IOException {
- if (this.file != null) {
- this.file.close();
- this.file = null;
- }
-
- }
-
- synchronized int read(long var1, byte[] var3, int var4, int var5) throws IOException {
- if (var1 != this.pos) {
- this.file.seek(var1);
- }
-
- int var6 = this.file.read(var3, var4, var5);
- if (var6 > 0) {
- this.pos = var1 + (long)var6;
- }
-
- return var6;
- }
-
- synchronized int read() throws IOException {
- if (this.pos != this.pos) {
- this.file.seek(this.pos);
- }
-
- int var1 = this.file.read();
- if (var1 > 0) {
- ++this.pos;
- }
-
- return var1;
- }
-
- private void init() throws ZipFormatException, IOException {
- if (!this.findEnd()) {
- throw new ZipFormatException("Could not find END header");
- } else {
- byte[] var1 = new byte[22];
- this.file.readFully(var1);
- if (!checkSig(var1, ZipConstants.ENDSIG)) {
- throw new ZipFormatException("Invalid END header signature");
- } else {
- this.cenpos = getLong(var1, 16);
- int var2 = (int)getLong(var1, 12);
- if (this.cenpos + (long)var2 != this.endpos) {
- throw new ZipFormatException("Invalid END header format");
- } else {
- int var3 = getWord(var1, 10);
- if (var3 * 46 > var2) {
- throw new ZipFormatException("Invalid END header format");
- } else if (getWord(var1, 8) != var3) {
- throw new ZipFormatException("Contains more than one drive");
- } else {
- this.file.seek(this.cenpos);
- byte[] var4 = new byte[var2];
- this.file.readFully(var4);
- this.table = new Hashtable(var3);
-
- int var6;
- for(int var5 = 0; var5 < var2; var5 += 46 + var6 + getWord(var4, var5 + 30) + getWord(var4, var5 + 32)) {
- if (!checkSig(var4, var5, ZipConstants.CENSIG)) {
- throw new ZipFormatException("Invalid CEN header signature");
- }
-
- var6 = getWord(var4, var5 + 28);
- if (var6 == 0 || var5 + 46 + var6 > var2) {
- throw new ZipFormatException("Invalid CEN header format");
- }
-
- String var7 = new String(var4, 0, var5 + 46, var6);
- ZipEntry var8 = new ZipEntry(var7);
- var8.locpos = getLong(var4, var5 + 42);
- if (var8.locpos + getLong(var4, var5 + 20) > this.cenpos) {
- throw new ZipFormatException("Invalid CEN header format");
- }
-
- var8.length = getLong(var4, var5 + 24);
- var8.mtime = getLong(var4, var5 + 12);
- if (this.table.put(var8.getPath(), var8) != null) {
- throw new ZipFormatException("Invalid CEN header format");
- }
- }
-
- if (this.table.size() != var3) {
- throw new ZipFormatException("Invalid CEN header format");
- }
- }
- }
- }
- }
- }
-
- private boolean findEnd() throws IOException {
- long var1 = this.file.length();
- this.file.seek(var1);
- long var3 = Math.max(0L, var1 - 65535L);
- byte[] var5 = new byte[68];
- setBytes(var5, 64, 4, 0);
- this.pos = var1;
-
- while(this.pos > var3) {
- int var6 = Math.min((int)(this.pos - var3), 64);
- this.pos -= (long)var6;
- this.file.seek(this.pos);
- this.file.readFully(var5, 0, var6);
-
- while(true) {
- --var6;
- if (var6 <= 0) {
- break;
- }
-
- if (checkSig(var5, var6, ZipConstants.ENDSIG)) {
- this.endpos = this.pos + (long)var6;
- if (var1 - this.endpos >= 22L) {
- this.file.seek(this.endpos);
- byte[] var7 = new byte[22];
- this.file.readFully(var7);
- int var8 = getWord(var7, 20);
- if (this.endpos + 22L + (long)var8 == var1) {
- this.file.seek(this.endpos);
- this.pos = this.endpos;
- return true;
- }
- }
- }
- }
- }
-
- return false;
- }
-
- static final boolean checkSig(byte[] var0, int var1, byte[] var2) {
- for(int var3 = 0; var3 < 4; ++var3) {
- if (var0[var1 + var3] != var2[var3]) {
- return false;
- }
- }
-
- return true;
- }
-
- static final boolean checkSig(byte[] var0, byte[] var1) {
- return checkSig(var0, 0, var1);
- }
-
- static final void setBytes(byte[] var0, int var1, int var2, int var3) {
- while(var2-- > 0) {
- var0[var1++] = (byte)var3;
- }
-
- }
-
- static final int getWord(byte[] var0, int var1) {
- return var0[var1] & 255 | (var0[var1 + 1] & 255) << 8;
- }
-
- static final long getLong(byte[] var0, int var1) {
- return (long)(getWord(var0, var1) | getWord(var0, var1 + 2) << 16);
- }
- }
-